<?php

namespace app\console\controller;

use think\Db;
use app\console\model\Group as GroupModel;
use app\console\validate\Group as GroupValidate;
use app\console\model\Admin as AdminModel;
use app\console\controller\Auth;

/**
 * 管理员分组
 * */
class Group extends Auth
{
    public function __construct()
    {
        parent::__construct();
        $this->assign('active', 'admin');
    }

    /**
     * 列表
     * */
    public function con_lists()
    {
        $map = [];
        $lists = GroupModel::page_lists($map);

        $this->assign([
            'topbar' => ['管理员', '用户组列表'],
            'lists' => $lists
        ]);

        return $this->fetch();
    }

    /**
     * 新增
     * */
    public function con_add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $check = (new GroupValidate())->gocheck($data);
            if ($check['code'] == 0) {
                $this->error($check['msg']);
            }
            GroupModel::create($data, true);
            $this->success('增加成功');
            exit;
        }
        $this->assign([
            'topbar' => ['管理员', '新增用户组'],
        ]);
        return $this->fetch();
    }

    /**
     * 编辑
     * */
    public function con_edit($id)
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $check = (new GroupValidate())->gocheck($data);
            if ($check['code'] == 0) $this->error($check['msg']);
            GroupModel::update($data, ['id' => $data['id']], true);
            $this->success('编辑成功');
            exit;
        }
        !isset($data['status']) && $data['status'] = 0;
        $that = GroupModel::get($id);
        $this->assign([
            'that' => $that,
            'topbar' => ['管理员', '编辑用户组'],
        ]);
        return $this->fetch();
    }

    /**
     * 权限
     * */
    public function con_powers($id)
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $data['pws'] = isset($data['pws']) ? implode(',', $data['pws']) : '';
            // 启动事务
            Db::startTrans();
            try {
                GroupModel::update(['powers' => $data['pws']], ['id' => $data['id']], true);
                AdminModel::update(['powers' => $data['pws']], ['belong_group_id' => $data['id']], true);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            $this->success('保存成功');
            exit;
        }

        $console_menu = AdminModel::user_left_bar();
        $group_powers_str = GroupModel::where(['id' => $id])->value('powers');
        $group_powers = explode(',', $group_powers_str);

        if (!$group_powers) $group_powers = [];

        $this->assign([
            'topbar' => ['用户组', '权限分配'],
            'console_menu' => $console_menu,
            'id' => $id,
            'group_powers' => json_encode($group_powers)
        ]);
        return $this->fetch();
    }


}
